package org.example.friend_test.service.impl;

import org.example.friend_test.entity.User;
import org.example.friend_test.mapper.UserMapper;
import org.example.friend_test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public User login(String username, String password) {
        return userMapper.findByUsernameAndPassword(username, password);
    }
    
    @Override
    public List<User> findAllUsers() {
        return userMapper.findAll();
    }
    
    @Override
    public User findUserById(Long id) {
        return userMapper.findById(id);
    }
    
    @Override
    public User findUserByUsername(String username) {
        return userMapper.findByUsername(username);
    }
    
    @Override
    public User createUser(User user) {
        user.setCreatedAt(LocalDateTime.now());
        user.setUpdatedAt(LocalDateTime.now());
        userMapper.insert(user);
        return user;
    }
    
    @Override
    public User updateUser(User user) {
        user.setUpdatedAt(LocalDateTime.now());
        userMapper.update(user);
        return user;
    }
    
    @Override
    public boolean deleteUser(Long id) {
        return userMapper.deleteById(id) > 0;
    }
}
